package net.wicp.tams.commons.binlog.event.rows.update;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.commons.apiext.ByteUtil;
import net.wicp.tams.commons.binlog.bean.EventBean;
import net.wicp.tams.commons.binlog.constant.ColumnType;
import net.wicp.tams.commons.binlog.event.rows.RowsEvent;
import net.wicp.tams.commons.callback.ICol;

@Slf4j
@SuppressWarnings("rawtypes")
public abstract class UpdateRowsEvent extends RowsEvent {

	private byte[] columnsPresentBitmap2;

	public UpdateRowsEvent(EventBean event) {
		super(event);
	}

	@Override
	public void parseUpdate() {
		if (version == 1 || version == 2) {
			int presentBitmap1Length = (columnCount.intValue() + 7) / 8;
			columnsPresentBitmap2 = readSubByte(presentBitmap1Length);
		}
	}

	@Override
	public void parseUpdateForRows() {
		if (version != 1 && version != 2) {
			return;
		}
		rowlog();
		int presentBitmapNum = ByteUtil.byteshas1(columnsPresentBitmap2);
		presentBitmapNum = presentBitmapNum > columnCount.intValue() ? columnCount.intValue() : presentBitmapNum;
		byte[] nulbitmap = readSubByte((presentBitmapNum + 7) / 8);
		int PresentIs1 = -1;
		for (int i = 0; i < table.getColumnCount(); i++) {
			if ((columnsPresentBitmap2[i / 8] & 0x01 << i % 8) != 0x00) {
				PresentIs1++;
				// 2、判断nulbitmap是否有
				if ((nulbitmap[PresentIs1 / 8] & 0x01 << (PresentIs1 % 8)) != 0x00) {
					System.out.println("是空值");
				} else {// 0 表示不是空值
					ColumnType colType = table.getColumnTypes()[i];
					ICol col = paseCol(colType, table.getMetaDefs()[i]);
					log.info("值【{}】", col.toClient());
				}
			} else {// 没有持有

			}
		}
	}

}
